package com.huluobo.a2301mdmdemo

import android.app.ActivityManager
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.SoundPool
import android.util.Log
import com.amap.api.location.AMapLocationClient
import com.huluobo.lib_base.BaseApp
import com.hyphenate.EMMessageListener
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMOptions
import com.hyphenate.chat.EMTextMessageBody
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp
import okhttp3.internal.notify


/**
 *  Created by LC on 2025/6/23.
 *
 */
@HiltAndroidApp
class MDMApp : BaseApp() {

    private val emMessageListener = EMMessageListener {
        showNotification(it)
        if (isForeground()) {
            soundPool.play(duan, 1f, 1f, 0, 0, 1f)
        } else {
            soundPool.play(yulu, 1f, 1f, 0, 0, 1f)
        }
    }

    private val soundPool = SoundPool.Builder().setMaxStreams(10).build()

    private var duan: Int = 0
    private var yulu: Int = 0

    private fun isForeground(): Boolean {
        //获取activity的管理器
        val activityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
        //找到当前正在运行的app的线程优先级
        activityManager.runningAppProcesses.forEach {
            //判断当前app运行的优先级是否等于前台优先级
            return it.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
        }
        return false
    }

    private fun showNotification(messages: List<EMMessage>) {
        //获取通知管理器
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        val channel = NotificationChannel("channel", "1", NotificationManager.IMPORTANCE_DEFAULT)
        notificationManager.createNotificationChannel(channel)
        messages.forEach {
            var contentText = "文本"
            if (it.type == EMMessage.Type.TXT) {
                //如果发送的消息是一条文本消息,转化成String给内容赋值
                contentText = (it.body as EMTextMessageBody).message
            } else if (it.type == EMMessage.Type.IMAGE) {
                contentText = "[图片]"
            } else if (it.type == EMMessage.Type.VOICE) {
                contentText = "[语音]"
            } else if (it.type == EMMessage.Type.FILE) {
                contentText = "[文件]"
            }

            val notification = Notification.Builder(this)
                .setContentTitle("收到新消息")
                .setSmallIcon(R.drawable.img)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.img))
                .setContentText(contentText)
                .setAutoCancel(true)
                .setChannelId("channel")
                .build()

            notificationManager.notify(1, notification)
        }
    }

    override fun onCreate() {
        super.onCreate()
        //MMKV初始化,需要在application的onCreate中进行
        MMKV.initialize(this)
        //高德地图初始化权限
        AMapLocationClient.updatePrivacyShow(this, true, true)
        AMapLocationClient.updatePrivacyAgree(this, true)

        val options = EMOptions()
        options.appKey = "moroks#2211mouthdemo"
        EMClient.getInstance().init(context, options);
        EMClient.getInstance().chatManager().addMessageListener(emMessageListener)
        duan = soundPool.load(this, R.raw.duan, 0)
        yulu = soundPool.load(this, R.raw.yulu, 0)
    }
}